package com.jxc.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Hibernate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jxc.dao.BaseDaoI;
import com.jxc.model.po.Repertory;
import com.jxc.model.po.Transferlist;
import com.jxc.model.po.TransferlistItem;
import com.jxc.model.po.User;
import com.jxc.model.vo.DataGrid;
import com.jxc.model.vo.VTransferlist;
import com.jxc.service.TransferlistServiceI;

@Service("transferlistService")
public class TransferlistServiceImpl implements TransferlistServiceI {

	@Autowired
	private BaseDaoI<Transferlist> transferlistDao;

	@Override
	public List<VTransferlist> combobox(VTransferlist vtl) {
		return this.changeModel(this.find(vtl));
	}

	@Override
	public List<VTransferlist> search(VTransferlist vtl) {
		List<Transferlist> tls = this.find(vtl);
		for (Transferlist tl : tls) {
			Hibernate.initialize(tl.getItems());
		}
		return this.changeModel(tls);
	}

	@Override
	public DataGrid datagrid(VTransferlist vtl) {
		vtl.setSort("date");
		vtl.setOrder("desc");
		DataGrid dg = new DataGrid();
		dg.setRows(this.changeModel(this.find(vtl)));
		dg.setTotal(this.total(vtl));
		return dg;
	}

	@Override
	public void delete(String ids) {
		// TODO 是否是物理删除,还要级联删除调拔单明细
		if (ids != null) {
			for (String id : ids.split(",")) {
				if (!id.trim().equals("0")) {
					Transferlist tl = transferlistDao.get(Transferlist.class, Long.parseLong(id.trim()));
					if (tl != null) {
						transferlistDao.delete(tl);
					}
				}
			}
		}
	}

	@Override
	public Long save(VTransferlist vtl) {
		Transferlist tl = new Transferlist();
		BeanUtils.copyProperties(vtl, tl);
		if (vtl.getUid() != null) {
			tl.setUser(new User(vtl.getUid()));
		}
		if (vtl.getIcuid() != null) {
			tl.setInConfirmUser(new User(vtl.getIcuid()));
		}
		if (vtl.getOcuid() != null) {
			tl.setOutConfirmUser(new User(vtl.getOrid()));
		}
		if (vtl.getIrid() != null) {
			tl.setInRepertory(new Repertory(vtl.getIrid()));
		}
		if (vtl.getOrid() != null) {
			tl.setOutRepertory(new Repertory(vtl.getOrid()));
		}
		tl.setIsInConfirm(false);
		tl.setIsOutConfirm(false);
		this.transferlistDao.save(tl);
		return tl.getId();
	}

	@Override
	public void update(VTransferlist vtl) {
		Transferlist tl = transferlistDao.get(Transferlist.class, vtl.getId());
		BeanUtils.copyProperties(vtl, tl, new String[] { "id" });
		if (vtl.getUid() != null) {
			tl.setUser(new User(vtl.getUid()));
		}
		if (vtl.getIcuid() != null) {
			tl.setInConfirmUser(new User(vtl.getIcuid()));
		}
		if (vtl.getOcuid() != null) {
			tl.setOutConfirmUser(new User(vtl.getOcuid()));
		}
		if (vtl.getIrid() != null) {
			tl.setInRepertory(new Repertory(vtl.getIrid()));
		}
		if (vtl.getOrid() != null) {
			tl.setOutRepertory(new Repertory(vtl.getOrid()));
		}
		this.transferlistDao.update(tl);
	}

	private List<VTransferlist> changeModel(List<Transferlist> tls) {
		List<VTransferlist> vtls = new ArrayList<VTransferlist>();
		if (tls != null && tls.size() > 0) {
			for (Transferlist tl : tls) {
				VTransferlist vtl = new VTransferlist();
				BeanUtils.copyProperties(tl, vtl);

				if (tl.getUser() != null) {
					User user = tl.getUser();
					vtl.setUid(user.getId());
					vtl.setUname(user.getUsername());
				}

				if (tl.getInConfirmUser() != null) {
					User icu = tl.getInConfirmUser();
					vtl.setIcuid(icu.getId());
					vtl.setIcuname(icu.getUsername());
				}
				
				if (tl.getOutConfirmUser() != null) {
					User ocu = tl.getOutConfirmUser();
					vtl.setOcuid(ocu.getId());
					vtl.setOcuname(ocu.getUsername());
				}

				if (tl.getOutRepertory() != null) {
					Repertory or = tl.getOutRepertory();
					vtl.setOrid(or.getId());
					vtl.setOrname(or.getName());
				}

				if (tl.getInRepertory() != null) {
					Repertory ir = tl.getInRepertory();
					vtl.setIrid(ir.getId());
					vtl.setIrname(ir.getName());
				}

				List<TransferlistItem> tlis = tl.getItems();

				vtls.add(vtl);
			}
		}
		return vtls;
	}

	private List<Transferlist> find(VTransferlist vtl) {
		String hql = "from Transferlist t where 1=1 ";

		List<Object> values = new ArrayList<Object>();
		hql = addWhere(vtl, hql, values);

		if (vtl.getSort() != null && vtl.getOrder() != null) {
			hql += " order by " + vtl.getSort() + " " + vtl.getOrder();
		}
		return transferlistDao.find(hql, values, vtl.getPage(), vtl.getRows());
	}

	private Long total(VTransferlist vtl) {
		String hql = "select count(*) from Transferlist t where 1=1 ";
		List<Object> values = new ArrayList<Object>();
		hql = addWhere(vtl, hql, values);
		return transferlistDao.count(hql, values);
	}

	private String addWhere(VTransferlist vtl, String hql, List<Object> values) {
		if (vtl.getId() != null) {
			hql += " and t.id = ? ";
			values.add(vtl.getId());
		}

		if (vtl.getSerial() != null) {
			hql += " and t.serial = ? ";
			values.add(vtl.getSerial());
		}

		if (vtl.getUid() != null) {
			hql += " and t.user.id = ? ";
			values.add(vtl.getUid());
		}

		if (vtl.getOcuid() != null) {
			hql += " and t.outConfirmUser.id = ? ";
			values.add(vtl.getOcuid());
		}

		if (vtl.getIcuid() != null) {
			hql += " and t.inConfirmUser.id = ? ";
			values.add(vtl.getIcuid());
		}

		if (vtl.getBeginDate() != null) {
			hql += " and t.date >= ? ";
			values.add(vtl.getBeginDate());
		}

		if (vtl.getEndDate() != null) {
			hql += " and t.date <= ? ";
			values.add(vtl.getEndDate());
		}

		if (vtl.getIrid() != null) {
			hql += " and t.inRepertory.id = ? ";
			values.add(vtl.getIrid());
		}

		if (vtl.getOrid() != null) {
			hql += " and ( t.outRepertory.id = ? or ( t.inRepertory.id = ? and t.isOutConfirm = ? ) ) ";
			values.add(vtl.getOrid());
			values.add(vtl.getOrid());
			values.add(true);
		}
		return hql;
	}

}
